package com.pubinfo.passbook.eop.impl;

import com.alibaba.fastjson.JSONObject;
import com.pubinfo.passbook.common.entity.TContractInvoiceApply;
import com.pubinfo.passbook.common.entity.TRequestLog;
import com.pubinfo.passbook.common.enums.ContractInvoiceStatusEnum;
import com.pubinfo.passbook.common.exception.ServiceException;
import com.pubinfo.passbook.common.model.constant.LogNameEnum;
import com.pubinfo.passbook.common.model.vo.base.BusinessErrorCode;
import com.pubinfo.passbook.common.service.contract.TContractInvoiceApplyService;
import com.pubinfo.passbook.common.service.master.TSyncLogService;
import com.pubinfo.passbook.eop.service.EopApiService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

@Slf4j
@Service
public class EopApiServiceImpl implements EopApiService {
  
  @Resource
  private TContractInvoiceApplyService contractInvoiceApplyService;
  @Resource
  private TSyncLogService logService;
  
  /**
   * 统一平台发票申请结果反刷
   *
   * @param systemNbr   系统编码
   * @param orderNum    电渠订单编号
   * @param codeNumber  业务号码
   * @param lanId       3.0地区id (杭州->8330100)
   * @param status      订单状态
   * @param errorInfo   如果报错，报错原因
   * @param bssOrderNum 申请发票编号
     * @param crmOrderNum crm单号
     */
    @Override
    @SneakyThrows
    public String invoiceRefresh(
            String systemNbr,
            String orderNum,
            String codeNumber,
            Long lanId,
            String status,
            String errorInfo,
            String bssOrderNum,
            String crmOrderNum
    ) {
  
      JSONObject jsonObject = new JSONObject();
      jsonObject.put("systemNbr", systemNbr);
      jsonObject.put("orderNum", orderNum);
      jsonObject.put("codeNumber", codeNumber);
      jsonObject.put("lanId", lanId);
      jsonObject.put("status", status);
      jsonObject.put("crmOrderNum", crmOrderNum);
      jsonObject.put("errorInfo", errorInfo);
      jsonObject.put("bssOrderNum", bssOrderNum);
  
      log.info("结果反刷参数->:{}", jsonObject);
  
      TContractInvoiceApply apply = contractInvoiceApplyService.lambdaQuery().eq(TContractInvoiceApply::getInvoiceApplyId, orderNum).one();
      if (Objects.isNull(apply)) {
        //记录到日志表，方便定位问题
        TRequestLog log = new TRequestLog();
        log.setType(1).setParam(jsonObject.toJSONString()).setUrl("eop/invoiceRefresh").setStatus(2).setName(LogNameEnum.EOP_INVOICE_REFRESH.getCode()).setRemark(BusinessErrorCode.INVOICE_APPLY_NOT_FUND.message);
        logService.save(log);
        throw new ServiceException(BusinessErrorCode.INVOICE_APPLY_NOT_FUND);
      }
      apply.setBssOrderNum(bssOrderNum);
      //更新开票状态
      apply.setRefreshJson(jsonObject);
      if (StringUtils.isNotBlank(errorInfo)) {
        apply.setStatus(ContractInvoiceStatusEnum.ERROR.getStatus());
      } else {
        apply.setStatus(ContractInvoiceStatusEnum.DONE.getStatus());
      }
      contractInvoiceApplyService.updateById(apply);
  
      //todo 。。。。。
      JSONObject result = new JSONObject();
      result.put("rtcode", 0);
      result.put("rtmsg", "SUCCESS");
      return result.toJSONString();
    }
}
